﻿using System;
using System.Collections.Generic;
using HTB.DevFx.Log;
using HTB.DevFx.Utils;
using Maple.Weixin.MP.Entities.Messages;

namespace Maple.Weixin.MP.Monitors
{
    public abstract class MonitorServiceBase : TimerBase, IMonitorService, IDisposable
    {
        protected virtual Queue<RequestMessageBase> BackupQueue { get; private set; }
        protected virtual Queue<RequestMessageBase> RequestMessageQueue { get; private set; }
        protected virtual bool Enabled { get; set; }
        private readonly object sync = new object();

        protected MonitorServiceBase() {
            this.BackupQueue = new Queue<RequestMessageBase>(1024);
            this.RequestMessageQueue = new Queue<RequestMessageBase>(1024);
        }

        // 定期引发记录监控数据的操作
        protected override void OnTimer() {
            var messageList = new List<RequestMessageBase>();
            var messageCounter = new Dictionary<string, int>();
            // 将两个队列调换位置
            lock (sync) {
                var temp = RequestMessageQueue;
                RequestMessageQueue = BackupQueue;
                BackupQueue = temp;
            }
            while (this.BackupQueue.Count > 0) {
                var message = BackupQueue.Dequeue();
                messageList.Add(message);
            }
            if (!this.Enabled || messageList.Count <= 0)
                return;

            var messages = messageList.ToArray();
            try {
                WriteReqeustMessages(messages);  // 记录消息列表
            }
            catch (Exception ex) {
                LogService.WriteLog(ex.Message + ex.StackTrace);
            }
        }

        protected virtual void WriteReqeustMessages(RequestMessageBase[] requestMessages) {
            foreach (var requestMessage in requestMessages) {
                LogService.WriteLog(JsonHelper.ToJson(requestMessage, false));
            }
        }

        protected virtual void ReordRequestMessage(RequestMessageBase requstMessage) {
            lock (RequestMessageQueue)
                RequestMessageQueue.Enqueue(requstMessage);
            this.StartTimer();
        }

        #region Implementation of IMonitorService

        /// <summary>
        /// 记录请求消息。
        /// </summary>
        /// <param name="requestMessage">所请求的消息。</param>
        void IMonitorService.RecordRequestMessage(RequestMessageBase requestMessage) {
            ReordRequestMessage(requestMessage);
        }

        #endregion

        #region Implementation of IDisposable

        private bool disposed;

        protected virtual void Disponse() {
            if (!this.disposed) {
                this.disposed = true;
                this.OnTimer();
            }
        }

        void IDisposable.Dispose() {
            this.Disponse();
            GC.SuppressFinalize(this);
        }

        ~MonitorServiceBase() {
            this.Disponse();
        }

        #endregion
    }
}
